package cn.com.egova.permission.inherit;

import android.Manifest;
import android.app.ActionBar;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import cn.com.egova.permission.R;


public class InheritMethodActivity extends BaseActivity {
    private static final String TAG = "InheritMethodActivity";
    private Button btn_call;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_inherit);

        initData();

        initActionBar();

        initView();
    }

    private void initData() {
        title = getIntent().getStringExtra("title");
        if(TextUtils.isEmpty(title)){
            title = "Permission";
        }
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        if(actionBar!=null){
            actionBar.setTitle(title);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
        }
    }

    private void initView() {
        btn_call = (Button) findViewById(R.id.btn_call);
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里不做版本号的判断也可以,不会报错,因为它默认会走onGranted()
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){  //23,Android 6.0
                    permission();
                }else{
                    call();
                }
            }
        });
    }


    private void permission(){
        requestRunPermisssion(new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.CAMERA}, new PermissionListener() {
            @Override
            public void onGranted() {
                Toast.makeText(InheritMethodActivity.this,"所有权限全部授权",Toast.LENGTH_SHORT).show();
                call();
            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                Toast.makeText(InheritMethodActivity.this,"未授权的权限包括:"+deniedPermission.toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void call(){
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri uri = Uri.parse("tel:" + "10086");
            intent.setData(uri);
            startActivity(intent);
            Toast.makeText(this,"打电话啦",Toast.LENGTH_SHORT).show();
        }catch (SecurityException e){
            e.printStackTrace();
        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }else{
            return super.onOptionsItemSelected(item);
        }
    }
}
